package com.ruoyi.campus.controller;

import com.ruoyi.campus.domain.SchoolInfo;
import com.ruoyi.campus.service.ISchoolInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学校信息Controller
 * 
 * @author campus-alarm
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/campus/school")
public class SchoolInfoController extends BaseController
{
    @Autowired
    private ISchoolInfoService schoolInfoService;

    /**
     * 查询学校信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(SchoolInfo schoolInfo)
    {
        startPage();
        List<SchoolInfo> list = schoolInfoService.selectSchoolInfoList(schoolInfo);
        return getDataTable(list);
    }

    /**
     * 查询启用状态的学校列表（用于下拉选择）
     */
    @PreAuthorize("@ss.hasPermi('campus:school:list')")
    @GetMapping("/listActive")
    public AjaxResult listActive()
    {
        List<SchoolInfo> list = schoolInfoService.selectActiveSchoolList();
        return AjaxResult.success(list);
    }

    /**
     * 根据地理位置查询附近学校
     */
    @PreAuthorize("@ss.hasPermi('campus:school:list')")
    @GetMapping("/listByLocation")
    public AjaxResult listByLocation(@RequestParam Double latitude, 
                                   @RequestParam Double longitude, 
                                   @RequestParam(defaultValue = "10") Double distance)
    {
        List<SchoolInfo> list = schoolInfoService.selectSchoolsByLocation(latitude, longitude, distance);
        return AjaxResult.success(list);
    }

    /**
     * 导出学校信息列表
     */
    @PreAuthorize("@ss.hasPermi('campus:school:export')")
    @Log(title = "学校信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SchoolInfo schoolInfo)
    {
        List<SchoolInfo> list = schoolInfoService.selectSchoolInfoList(schoolInfo);
        ExcelUtil<SchoolInfo> util = new ExcelUtil<SchoolInfo>(SchoolInfo.class);
        util.exportExcel(response, list, "学校信息数据");
    }

    /**
     * 获取学校信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:school:query')")
    @GetMapping(value = "/{schoolId}")
    public AjaxResult getInfo(@PathVariable("schoolId") Long schoolId)
    {
        return AjaxResult.success(schoolInfoService.selectSchoolInfoBySchoolId(schoolId));
    }

    /**
     * 获取学校信息详细信息（包含统计数据）
     */
    @PreAuthorize("@ss.hasPermi('campus:school:query')")
    @GetMapping(value = "/detail/{schoolId}")
    public AjaxResult getDetailInfo(@PathVariable("schoolId") Long schoolId)
    {
        return AjaxResult.success(schoolInfoService.selectSchoolInfoWithStatistics(schoolId));
    }

    /**
     * 新增学校信息
     */
    @PreAuthorize("@ss.hasPermi('campus:school:add')")
    @Log(title = "学校信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SchoolInfo schoolInfo)
    {
        schoolInfo.setCreateBy(getUsername());
        return toAjax(schoolInfoService.insertSchoolInfo(schoolInfo));
    }

    /**
     * 修改学校信息
     */
    @PreAuthorize("@ss.hasPermi('campus:school:edit')")
    @Log(title = "学校信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SchoolInfo schoolInfo)
    {
        schoolInfo.setUpdateBy(getUsername());
        return toAjax(schoolInfoService.updateSchoolInfo(schoolInfo));
    }

    /**
     * 删除学校信息
     */
    @PreAuthorize("@ss.hasPermi('campus:school:remove')")
    @Log(title = "学校信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schoolIds}")
    public AjaxResult remove(@PathVariable Long[] schoolIds)
    {
        return toAjax(schoolInfoService.deleteSchoolInfoBySchoolIds(schoolIds));
    }

    /**
     * 校验学校编码
     */
    @PostMapping("/checkSchoolCodeUnique")
    public AjaxResult checkSchoolCodeUnique(@RequestBody SchoolInfo schoolInfo)
    {
        String result = schoolInfoService.checkSchoolCodeUnique(schoolInfo);
        return AjaxResult.success(result);
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('campus:school:edit')")
    @Log(title = "学校信息", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SchoolInfo schoolInfo)
    {
        return toAjax(schoolInfoService.changeSchoolStatus(schoolInfo.getSchoolId(), schoolInfo.getStatus()));
    }

    /**
     * 批量更新状态
     */
    @PreAuthorize("@ss.hasPermi('campus:school:edit')")
    @Log(title = "学校信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatusBatch")
    public AjaxResult updateStatusBatch(@RequestParam Long[] schoolIds, @RequestParam Integer status)
    {
        return toAjax(schoolInfoService.updateSchoolStatusBatch(schoolIds, status));
    }

    /**
     * 导入学校信息
     */
    @PreAuthorize("@ss.hasPermi('campus:school:import')")
    @Log(title = "学校信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SchoolInfo> util = new ExcelUtil<SchoolInfo>(SchoolInfo.class);
        List<SchoolInfo> schoolList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = schoolInfoService.importSchool(schoolList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SchoolInfo> util = new ExcelUtil<SchoolInfo>(SchoolInfo.class);
        util.importTemplateExcel(response, "学校信息数据");
    }
} 